<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="adminName">
      <el-input v-model="dataForm.adminName" placeholder="管理者名称"></el-input>
    </el-form-item>
    <el-form-item label="性别 " prop="adminGender">
      <!-- <el-input v-model="dataForm.adminGender" placeholder="管理者性别 0：男 1：女"></el-input> -->
    <el-select v-model="dataForm.adminGender" placeholder="管理者性别 0：男 1：女">
					<el-option label="男" value="0"></el-option>
					<el-option label="女" value="1"></el-option>
				</el-select>
    </el-form-item>
    <el-form-item label="电话号码" prop="adminPhone">
      <el-input v-model="dataForm.adminPhone" placeholder="管理者电话号码"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="adminPic">
      <el-input v-model="dataForm.adminPic" placeholder="管理者图片"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item> -->
    <el-form-item label="状态" prop="adminStatus">
      <!-- <el-input v-model="dataForm.adminStatus" placeholder="状态 0：存在 1：删除"></el-input> -->
        <el-select v-model="dataForm.adminStatus" placeholder="状态 0：存在 1：删除">
					<el-option label="存在" value="0"></el-option>
					<el-option label="删除" value="1"></el-option>
				</el-select>
    </el-form-item>
    <el-form-item label="是否删除" prop="delFlag">
      <!-- <el-input v-model="dataForm.delFlag" placeholder="是否删除 0：可用 1：停用"></el-input> -->
      	<el-select v-model="dataForm.delFlag" placeholder="是否删除 0：可用 1：停用">
					<el-option label="可用" value="0"></el-option>
					<el-option label="停用" value="1"></el-option>
				</el-select>
    </el-form-item>
    <el-form-item label="排序码" prop="adminSort">
      <el-input v-model="dataForm.adminSort" readonly="true" placeholder="排序码"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建者" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建者"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          adminId: 0,
          adminName: '',
          adminGender: '',
          adminPhone: '',
          adminPic: '',
          createTime: '',
          updateTime: '',
          adminStatus: '',
          delFlag: '',
          adminSort: '',
          createUser: ''
        },
        dataRule: {
          adminName: [
            { required: true, message: '管理者名称不能为空', trigger: 'blur' }
          ],
          adminGender: [
            { required: true, message: '管理者性别 0：男 1：女不能为空', trigger: 'blur' }
          ],
          adminPhone: [
            { required: true, message: '管理者电话号码不能为空', trigger: 'blur' }
          ],
          adminPic: [
            { required: true, message: '管理者图片不能为空', trigger: 'blur' }
          ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // updateTime: [
          //   { required: true, message: '更新时间不能为空', trigger: 'blur' }
          // ],
          adminStatus: [
            { required: true, message: '状态 0：存在 1：删除不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '是否删除 0：可用 1：停用不能为空', trigger: 'blur' }
          ],
          adminSort: [
            { required: true, message: '排序码不能为空', trigger: 'blur' }
          ],
          // createUser: [
          //   { required: true, message: '创建者不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.adminId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.adminId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webadministrator/info/${this.dataForm.adminId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.adminName = data.webAdministrator.adminName
                this.dataForm.adminGender = data.webAdministrator.adminGender
                this.dataForm.adminPhone = data.webAdministrator.adminPhone
                this.dataForm.adminPic = data.webAdministrator.adminPic
                this.dataForm.createTime = data.webAdministrator.createTime
                this.dataForm.updateTime = data.webAdministrator.updateTime
                this.dataForm.adminStatus = data.webAdministrator.adminStatus
                this.dataForm.delFlag = data.webAdministrator.delFlag
                this.dataForm.adminSort = data.webAdministrator.adminSort
                this.dataForm.createUser = data.webAdministrator.createUser
              }

            })
          }
             else{
            console.log('新增管理员');
             this.$http({
              url: this.$http.adornUrl(`/generator/webadministrator/getMaxSort`),
              method: 'get'
            }).then(({data}) => {
              if (data!=null) {
                 console.log('最大排序码是：'+data);
                 this.dataForm.adminSort=data+1;

              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webadministrator/${!this.dataForm.adminId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'adminId': this.dataForm.adminId || undefined,
                'adminName': this.dataForm.adminName,
                'adminGender': this.dataForm.adminGender,
                'adminPhone': this.dataForm.adminPhone,
                'adminPic': this.dataForm.adminPic,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'adminStatus': this.dataForm.adminStatus,
                'delFlag': this.dataForm.delFlag,
                'adminSort': this.dataForm.adminSort,
                'createUser': this.dataForm.createUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
